package concurrecy;

import java.util.concurrent.TimeUnit;

class Sleeper extends Thread {

    private String name;

    public Sleeper(String name) {
        this.name = name;
        start();
    }

    @Override
    public void run() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            System.out.println(name + " interrupted, " + isInterrupted());
            return;
        }
        System.out.println(name + " sleep completed");
    }
}

class Joiner extends Thread {

    private Thread joinThread;
    private String name;

    public Joiner(String name, Thread joinThread) {
        this.name = name;
        this.joinThread = joinThread;
        start();
    }

    @Override
    public void run() {
        try {
            joinThread.join();
        } catch (InterruptedException e) {
            System.out.println(name + " interrupted, " + isInterrupted());
            return;
        }
        System.out.println(name + " join completed");
    }
}

public class JoinDemo {
    public static void main(String[] args) {
        Sleeper sleeper1 = new Sleeper("Sleeper1");
        Sleeper sleeper2 = new Sleeper("Sleeper2");

        Joiner joiner1 = new Joiner("Joiner1", sleeper1);
        Joiner joiner2 = new Joiner("Joiner2", sleeper2);

        sleeper1.interrupt();
    }
}
